package com.mongdbtokuduswagger.utils;

import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Daft_V
 * @date 2022/3/10 17:30
 */
public class DateUtil {
    public static List<String> getCalendarList(String startDate, String endDate, String days) throws ParseException {
        List<String> calendarList = new ArrayList<>();
        Set<Integer> weekSet = new HashSet<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat resultFormat = new SimpleDateFormat("yyyyMMdd");
        Date startDay = dateFormat.parse(startDate);
        Date endDay = dateFormat.parse(endDate);
        //days字段的第一位往后(包括第一位)表示符合条件的星期，1~7分别表示周一至周日
        for (int i = 0; i < days.length(); i++) {
            Integer week = Integer.valueOf(days.substring(i, i + 1));
            if (week == 7) {
                weekSet.add(1);
            } else {
                weekSet.add(++week);
            }
        }
        //先判断开始日期是否是符合条件的星期
        if (weekSet.contains(getDayOfWeek(startDay))) {
            calendarList.add(resultFormat.format(startDay));
        }
        if (!DateUtils.isSameDay(startDay, endDay)) {
            //开始日期与结束日期不是同一天
            do {
                //天数加一
                startDay = DateUtils.addDays(startDay, 1);
                //判断新的日期是否是符合条件的星期
                if (weekSet.contains(getDayOfWeek(startDay))) {
                    calendarList.add(resultFormat.format(startDay));
                }
            } while (!DateUtils.isSameDay(startDay, endDay));
        }
        return calendarList;
    }

    //根据日期获取该日期所对应的星期数
    public static int getDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    //返回结束时间与开始开始之间的小时数
    public static Double getHours(String endTime, String startTime) throws ParseException {
        Double hours = 0.0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date overTime = dateFormat.parse(endTime);
        Date beginTime = dateFormat.parse(startTime);
        if (beginTime.getTime() > overTime.getTime()) {
            //开始时间点大于结束时间点，结束时间点就向后推24h
            overTime = DateUtils.addDays(overTime, 1);
        }
        do {
            hours++;
            beginTime = DateUtils.addHours(beginTime, 1);
        } while (overTime.getTime() > beginTime.getTime());
        //此时如果结束时间点小于开始时间点
        if (overTime.getTime() < beginTime.getTime()) {
            hours -= (double) (beginTime.getTime() - overTime.getTime()) / 1000 / 3600;
        }
        return hours;
    }

    /**
     * 获取当前时间前一天
     */
    public static String getLastDate(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date end = c.getTime();
        //当前日期
        String dqrq= format.format(end);

        c.add(Calendar.DATE, -1);
        Date start = c.getTime();
        //前一天
        String qyt= format.format(start);

        return qyt;
    }

    /**
     * 获取当前时间前几天
     */
    public static String getLastTime(int number){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date end = c.getTime();
        //当前日期
        String dqrq= format.format(end);

        c.add(Calendar.DATE, -number);
        Date start = c.getTime();
        //前一天
        String qyt= format.format(start);

        return qyt;
    }

    /**
     * 获取当前时间前一天(标准时间)
     */
    public static String getLastDateStand(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date end = c.getTime();
        //当前日期
        String dqrq= format.format(end);

        c.add(Calendar.DATE, -1);
        Date start = c.getTime();
        //前一天
        String qyt= format.format(start);

        return qyt;
    }

    //返回结束时间与开始开始之间的小时数
    public static Double getHoursByStand(String endTime, String startTime) throws ParseException {
        Double hours = 0.0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date overTime = dateFormat.parse(endTime);
        Date beginTime = dateFormat.parse(startTime);
        if (beginTime.getTime() > overTime.getTime()) {
            //开始时间点大于结束时间点，结束时间点就向后推24h
            overTime = DateUtils.addDays(overTime, 1);
        }
        do {
            hours++;
            beginTime = DateUtils.addHours(beginTime, 1);
        } while (overTime.getTime() > beginTime.getTime());
        //此时如果结束时间点小于开始时间点
        if (overTime.getTime() < beginTime.getTime()) {
            hours -= (double) (beginTime.getTime() - overTime.getTime()) / 1000 / 3600;
        }
        return hours;
    }
}
